package com.wd.health.model.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wd.health.R;
import com.wd.health.model.bean.ZixunBean;
import com.wd.health.view.activity.ZxxqActivity;
import com.facebook.drawee.view.SimpleDraweeView;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @ProjectName:shopmall
 * @Package:com.bawei.myhealth.model.adapter
 * @ClassName:ZixunAdapter
 * @UpdateUser: 裴童童
 * @date: 2020/12/8
 */
public class ZixunAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    public static final int TYPE_ONE = 1;
    public static final int TYPE_TWO = 2;
    private Context context;
    private SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
    private String[] split = new String[]{};
    private List<ZixunBean.ResultBean> list;

    public ZixunAdapter(Context context, List<ZixunBean.ResultBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_ONE){
            return new ViewHolder01(LayoutInflater.from(context).inflate(R.layout.item_type01, parent, false));
        }else {
            return new ViewHolder02(LayoutInflater.from(context).inflate(R.layout.item_type02, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder01){
            final ViewHolder01 viewHolder01 = (ViewHolder01) holder;
            final ZixunBean.ResultBean bean = list.get(position);
            viewHolder01.sdv1.setImageURI(split[0]);
            viewHolder01.sdv2.setImageURI(split[1]);
            viewHolder01.sdv3.setImageURI(split[2]);
            viewHolder01.tv_time.setText(bean.getSource());
            String format = simpleDateFormat.format(bean.getReleaseTime());
            viewHolder01.tv_time.setText(format+"");
            viewHolder01.tv_tm.setText(bean.getTitle());
            viewHolder01.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(viewHolder01.itemView.getContext(), ZxxqActivity.class);
                    intent.putExtra("id",bean.getId());
                    viewHolder01.itemView.getContext().startActivity(intent);
                }
            });
        }else {
            ViewHolder02 viewHolder02 = (ViewHolder02) holder;
            ZixunBean.ResultBean resultBean = list.get(position);
            viewHolder02.sdv4.setImageURI(split[0]);
            viewHolder02.tv_zixun_weidu02.setText(resultBean.getSource());
            String format = simpleDateFormat.format(resultBean.getReleaseTime());
            viewHolder02.tv_zixun_time02.setText(format+"");
            viewHolder02.tv_zixun_tm02.setText(resultBean.getTitle());
        }
    }

    @Override
    public int getItemViewType(int position) {
        split = list.get(position).getThumbnail().split(";");
        if (split.length == 1) {
            return TYPE_TWO;
        } else {
            return TYPE_ONE;
        }
    }

    @Override
    public int getItemCount() {
       return list.size();
    }

    class ViewHolder01 extends RecyclerView.ViewHolder {

        private final SimpleDraweeView sdv1,sdv2,sdv3;
        private final TextView tv_tm;
        private final TextView tv_weidu;
        private final TextView tv_time;

        public ViewHolder01(@NonNull View itemView) {
            super(itemView);
            sdv1 = itemView.findViewById(R.id.sdv1);
            sdv2 = itemView.findViewById(R.id.sdv2);
            sdv3 = itemView.findViewById(R.id.sdv3);
            tv_tm = itemView.findViewById(R.id.tv_zixun_tm);
            tv_weidu = itemView.findViewById(R.id.tv_zixun_weidu);
            tv_time = itemView.findViewById(R.id.tv_zixun_time);
        }
    }
    class ViewHolder02 extends RecyclerView.ViewHolder {

        private final TextView tv_zixun_time02;
        private final SimpleDraweeView sdv4;
        private final TextView tv_zixun_tm02;
        private final TextView tv_zixun_weidu02;

        public ViewHolder02(@NonNull View itemView) {
            super(itemView);
            tv_zixun_time02 = itemView.findViewById(R.id.tv_zixun_time02);
            sdv4 = itemView.findViewById(R.id.sdv4);
            tv_zixun_tm02 = itemView.findViewById(R.id.tv_zixun_tm02);
            tv_zixun_weidu02 = itemView.findViewById(R.id.tv_zixun_weidu02);
        }
    }
} 
